package com.hxs.learn.service.core;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.constants.core.sql.SqlColConts;
import com.hxs.learn.entity.core.SysMenu;
import com.hxs.learn.entity.core.SysRoleMenu;
import com.hxs.learn.mapper.core.SysRoleMenuMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author huxinsheng
 */
@Service
public class SysRoleMenuService {
    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    private void buildMenuTreeGrid(List<SysMenu> menus, List<TreeJson<Object>> treeJsonList, String parentId) {
        if (!CollectionUtils.isEmpty(menus)) {
            for (SysMenu sysMenu : menus) {
                TreeJson treeJson = new TreeJson<SysMenu>();
                String id = parentId + "-" + sysMenu.getId();

                treeJson.setId(id);
                treeJson.setPid(parentId);
                treeJson.setName(sysMenu.getName());
                treeJson.setData(sysMenu);
                treeJson.setSeq(sysMenu.getSeq());
                treeJsonList.add(treeJson);

                List<SysMenu> subMenus = sysMenu.getSubMenus();

                if (!CollectionUtils.isEmpty(subMenus)) {
                    sysMenu.setSubMenus(null);
                    buildMenuTreeGrid(subMenus, treeJsonList, id);
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Result saveRoleMenu(LoginInfo userInfo, SysRoleMenu roleMenu) {
        String menuId = roleMenu.getMenuId();
        String roleId = roleMenu.getRoleId();
        String[] menuIds = StringUtils.split(menuId, ",");

        if (ArrayUtils.isEmpty(menuIds)) {
            return Result.error("该角色未添加任何菜单");
        }

        EntityWrapper<SysRoleMenu> wrapper = new EntityWrapper<>();

        wrapper.eq(SqlColConts.ROLE_ID, roleId);

        // 先删除之前关联的菜单数据
        roleMenuMapper.delete(wrapper);

        List<SysRoleMenu> pojos = new ArrayList<>(menuIds.length);

        for (String id : menuIds) {
            SysRoleMenu pojo = new SysRoleMenu();

            pojo.setMenuId(id);
            pojo.setRoleId(roleId);
            pojo.setCreateTime(new Date());
            pojo.setCreator(userInfo.getLoginName());
            pojos.add(pojo);
        }

        roleMenuMapper.insertAll(pojos);

        return Result.ok();
    }

    public List<TreeJson<Object>> getMenuTreeJsonByRoleId(String roleId) {
        List<SysMenu> menus = roleMenuMapper.selectByRoleIdAndParentId(roleId, SqlColConts.PID_VALUE);

        if (CollectionUtils.isEmpty(menus)) {
            return null;
        }

        List<TreeJson<Object>> treeJsonList = new LinkedList<>();

        buildMenuTreeGrid(menus, treeJsonList, roleId);

        return treeJsonList;
    }

    public List<TreeJson<Object>> getRoleMenuTreeJson(String roleId) {
        return roleMenuMapper.selectRoleMenuTreeJsonByRoleId(roleId, SqlColConts.PID_VALUE);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
